<?php

 	class CSession
    {
		var $m_hCookie;				//cookie manager
		
		var $m_szSid;		  		//session id
		var $m_hVar = array();		//user variable array
		
		var $m_iCurrTm;				//obecny czas systemowy
		var $iTtl;					//session time to live
		
		 static function &GetInstance()
		 {
			global $g_hObjects;
			static $m_hApp = NULL;
						 
			if(!isset($m_hApp))
			{
				$m_hApp       = new CSession();					 
				$g_hObjects[] = $m_hApp;
				$m_hApp->OnInit();
			}
			
			return $m_hApp;
		}
		
		function OnInit()
		{	
			$this->iTtl	     = SESSION_TTL;	//defaultowy ttl
			$this->m_iCurrTm = xtime(); //defaultowy czas systemowy

			//zbieramy nazwe sesji
			$this->m_hCookie = CCookie::GetInstance();
			$this->m_szSid   = $this->m_hCookie->Get(SID,32);
			$this->m_szSid   = strrev($this->m_szSid);	
		}
		
		function OnDestroy()
		{    	
			//ladujemy content sesji do pliku
			$szPath = SESSION_DIR.'/'.$this->m_szSid.'.sid';
		
			$iTtl  = $this->m_iCurrTm;
			$iTtl += $this->iTtl;
			
			file_put_contents($szPath,serialize($this->m_hVar));
			@chmod($szPath,0666);
			@touch($szPath,$iTtl,$iTtl);
			 
			
			//odpalamy czyszczenie smieci 
			$iTemp = rand(1,30);
			
			if($iTemp != 15) { return; }
							
			//obecna sciezka do sesji
			$szCurrSessionFile = $this->m_szSid.'.sid';
			
			$hDir  = opendir(SESSION_DIR);
			$iTime = $this->m_iCurrTm - 3*24*60*60;
			
			while(($szFile = readdir($hDir)) !== false)
			{
				if($szFile[0] == '.' || $szFile == '..' || $szFile == $szCurrSessionFile)
					continue;
				
				$szPath = SESSION_DIR.'/'.$szFile;
				
				if(date('U',filemtime($szPath)) <= $iTime)
					unlink($szPath);
			}
			
			closedir($hDir);
		}
	
   		//dodaje zmienna do sesji
		function AddVar($szKey,$szVal)
		{    
			$this->m_hVar[$szKey] = $szVal;	    
		}
		
		//kasuje zmienna w sesji
		function GetVar($szKey)
		{
			if(isset($this->m_hVar[$szKey]))
			{
				return $this->m_hVar[$szKey];
			}
			
			return '';
		}
		
		//remove user variable
		function DelVar($szKey)
		{
			if(isset($this->m_hVar[$szKey]))
			{
				unset($this->m_hVar[$szKey]);
			}
		}
		
		//zwraca cala tablice parametrow
    	function &GetVarArray()
		{
			return $this->m_hVar;
		}
		
		//ustawia waznosc sesji na zadany czas
		// $iTtl - time to live w sekundach 
    	function SetTtl($iTtl)
		{
			$this->iTtl = $iTtl;
		}
		
    	//zwraca unikatowy identyfikator sesji
		function GenerateSid()
		{
			while(1)
			{
				$szHash = md5(uniqid(rand(),true));
					
				if(file_exists(SESSION_DIR.'/'.$szHash.'.sid'))
					continue;
				else
					break;
			}
			
			return $szHash;
		}
		
		//odpalamy obsluge sesji
		function Start()
		{
			$szPath = SESSION_DIR.'/'.$this->m_szSid.'.sid';
		
			if(file_exists($szPath) && date('U', filemtime($szPath)) >= $this->m_iCurrTm)
			{
				$szData       = file_get_contents($szPath);
				$this->m_hVar = unserialize($szData);
				
				$iTtl  = $this->m_iCurrTm;
				$iTtl += $this->iTtl;
				touch($szPath,$iTtl,$iTtl);
				
				$this->m_hCookie->Set(SID,strrev($this->m_szSid));
			}
			else
			{	
				//generujemy nowego sid'a
				$this->m_szSid = $this->GenerateSid();
				
				//zapodaj sid'a w pliku cookie
				$this->m_hCookie->Set(SID,strrev($this->m_szSid));
			}
			
			//prev url
			$this->m_hVar['prev_url'] = isset($this->m_hVar['url']) ? $this->m_hVar['url'] : '';
			
			//uzupelniamy dane o kliencie
			$this->m_hVar['url']   = CRequest::GetRequestedUrl();
			$this->m_hVar['host']  = CRequest::GetUserIp();
			$this->m_hVar['agent'] = CRequest::GetUserAgent();
			$this->m_hVar['url']   = CRequest::GetRequestedUrl();
			
			$this->iTtl =  (isset($this->m_hVar['ttl']) && $this->m_hVar['ttl'] > $this->iTtl) ? $this->m_hVar['ttl'] : $this->iTtl;
			
			$this->m_hVar['ttl'] = $this->iTtl;
		}
    }

?>